<template>
	<view class="container">
		<!-- 搜索栏区域 -->
		<view class="search-bar">
			<image class="map-icon"
				src="https://cdn8.axureshop.com/demo/1689476/images/%E7%BB%84%E4%BB%B6_%E8%A7%84%E8%8C%83/u1309.png">
			</image>
			<view class="search-wrap" @click="goToSearch">
				<image class="search-icon" src="/static/search.png"></image>
				<input class="search-input" placeholder="搜索机构/医生" placeholder-style="color: #999" disabled />
			</view>
		</view>

		<view class="institution-list">
			<navigator :url="`/homepackge/pages/organizationalPage/organizationalPage?_id=${item._id}`"
				v-for="(item, index) in organizations" :key="item._id || index" class="institution-card-link">
				<view class="institution-card">
					<view class="card-left">
						<image class="hospital-icon" :src="item.image"></image>
					</view>
					<view class="card-middle">
						<text class="institution-name">{{ item.name }}</text>
						<text class="distance">{{ item.address }} 距您{{ item.distance }}km</text>
					</view>
					<view class="card-right">
						<image class="arrow-icon" src="/static/arrow-right.png"></image>
					</view>
				</view>
			</navigator>
		</view>
	</view>
</template>

<script setup>
import {
	onMounted,
	ref
} from 'vue';
import {
	getAllOrganizationApi
} from '../../../api/home';

const organizations = ref([]);
const loading = ref(false);

onMounted(() => {
	getAllOrganizations();
})

// 获取所有机构
const getAllOrganizations = async () => {
	loading.value = true;
	const res = await getAllOrganizationApi();

	if (res.code === 200) {
		organizations.value = res.data.rows || [];
	} else {
		uni.showToast({
			title: res.message || '获取机构列表失败',
			icon: 'none'
		});
	}

	loading.value = false;
}

// 跳转到搜索页面
const goToSearch = () => {
	uni.navigateTo({
		url: '/pages/search-organizations-or-doctors/search-organizations-or-doctors'
	})
}
</script>

<style scoped lang="scss">
.container {
	min-height: 100vh;
	background-color: #f5f5f5;
}

/* 搜索栏样式 */
.search-bar {
	display: flex;
	align-items: center;
	padding: 10px 15px;
	background-color: #fff;
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.03);

	.map-icon {
		width: 24px;
		height: 24px;
		margin-right: 10px;
	}

	.search-wrap {
		display: flex;
		align-items: center;
		flex: 1;
		background-color: #f5f5f5;
		border-radius: 20px;
		padding: 6px 12px;
		cursor: pointer;
		transition: background-color 0.2s ease;
	}

	.search-wrap:active {
		background-color: #e8e8e8;
	}

	.search-icon {
		width: 18px;
		height: 18px;
		margin-right: 8px;
	}

	.search-input {
		flex: 1;
		font-size: 14px;
		height: 24px;
		padding: 0;
	}
}

/* 机构列表容器 */
.institution-list {
	padding: 15px 10px;
}

/* 机构卡片链接 */
.institution-card-link {
	display: block;
	margin-bottom: 10px;
	text-decoration: none;
}

.institution-card {
	display: flex;
	align-items: center;
	justify-content: space-between;
	background-color: #fff;
	border-radius: 8px;
	padding: 15px;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
	transition: all 0.2s ease;
}

/* 点击效果 */
.institution-card:active {
	background-color: #f8f8f8;
	transform: scale(0.98);
}

.card-left .hospital-icon {
	width: 48px;
	height: 48px;
	margin-right: 12px;
	border-radius: 4px;
}

.card-middle {
	flex: 1;
	min-width: 0;

	.institution-name {
		font-size: 16px;
		font-weight: 500;
		color: #333;
		line-height: 1.4;
		margin-bottom: 4px;
		display: block;
		white-space: rap;

	}

	.distance {
		font-size: 14px;
		color: #999;
	}
}

/* 卡片右侧箭头 */
.card-right .arrow-icon {
	width: 20px;
	height: 20px;
	color: #999;
	margin-left: 10px;
}
</style>